package com.lf.lexy.business;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.lf.comm.MyConstants;
import com.lf.lexy.dao.BlogContentDao;
import com.lf.lexy.dao.BlogTypeDao;
import com.lf.lexy.model.BlogContent;
import com.lf.lexy.model.BlogType;
import com.lf.lexy.model.User;
import com.lf.lexy.webutils.CommMethod;
import com.lf.lexy.webutils.DateUtils;
import com.lf.lexy.webutils.GetWaterId;

public class BlogBusiness {
	
	
	
	
	
	/**
	 * @param map
	 */
	public String insertBlogCotent(Map map , User user){
		BlogContent bcn = new BlogContent();
		String serialId = GetWaterId.getWaterId();
		bcn.setSerialId(serialId);
		bcn.setUserNo(user.getUserNo());
		bcn.setUserName(user.getUserName());
		bcn.setBlogTitle(String.valueOf(map.get("blogTitle")));
		bcn.setBlogContent(String.valueOf(map.get("blogContent")).replace("|RP|", "&nbsp;"));///&nbsp;/g, "|RP|"
		bcn.setUpdateUserNo(user.getUserNo());
		bcn.setUpdateTime(DateUtils.getCurrDateTime());
		bcn.setIsDelete("0");
		String typeNo = String.valueOf(map.get("typeNo"));
		BlogType blogType = BlogTypeDao.me.geetBlogTypeByNo(typeNo, "0");
		bcn.setTypeNo(typeNo);
		bcn.setTypeName(blogType.getTypeName());
		bcn.setShowType(String.valueOf(map.get("showType")));
		BlogContentDao.me.insertBlogContent(bcn);
		return serialId;
	}
	
	/**
	 * @param map
	 * @param user
	 */
	public String updateBlogCotent(Map map , User user){
		String serialid = (String)map.get("serialId");
		BlogContent bcn = BlogContentDao.me.geetBlogContentByPK(serialid);
		bcn.setUserNo(user.getUserNo());
		bcn.setUserName(user.getUserName());
		bcn.setBlogTitle(String.valueOf(map.get("blogTitle")));
		bcn.setBlogContent(String.valueOf(map.get("blogContent")).replace("|RP|", "&nbsp;"));///&nbsp;/g, "|RP|"
		bcn.setUpdateUserNo(user.getUserNo());
		bcn.setUpdateTime(DateUtils.getCurrDateTime());
		bcn.setIsDelete("0");
		
		String typeNo = String.valueOf(map.get("typeNo"));
		BlogType blogType = BlogTypeDao.me.geetBlogTypeByNo(typeNo, "0");
		bcn.setTypeNo(typeNo);
		bcn.setTypeName(blogType.getTypeName());
		bcn.setShowType(String.valueOf(map.get("showType")));
		bcn.update();
		return serialid;
	}
	
	
	
	
	
	
	
	
	
	
	public Page<BlogContent> getBlogContentList(String userNo , String typeNo ,  String pageNumber , int pagesize){
		Page<BlogContent> blogContentList = BlogContentDao.me.getBlogContentList(userNo, typeNo, pageNumber , pagesize);
		
		List<BlogContent> list = blogContentList.getList();
		
		List<BlogContent> exeList = new ArrayList<BlogContent>();
		if(list!=null && list.size()>0){
			
			for (BlogContent blogContent : list) {
				String blogContent2 = blogContent.getBlogContent();
				if(blogContent2.length()>50){
					blogContent.setBlogContent(blogContent2.substring(50)+"......");
				}
				exeList.add(blogContent);
			}
		}
		return	new Page<BlogContent>(exeList, blogContentList.getPageNumber(), blogContentList.getPageSize(), blogContentList.getTotalPage(), blogContentList.getTotalRow());
		
	}
	
	
	public  List<Record>   getBlogTypeList(String userNo){
		return BlogContentDao.me.getBlogTypeList(userNo);
	}
	
	
	
	public String fileUpload(UploadFile file , String serialId){
		
		int length = 0;
		
		String result = "";
		
		File file2 = file.getFile();
		FileInputStream fis = null;
		FileOutputStream fos = null;
		
		
		try {
			
			String uploadPath = file.getUploadPath();
			
			String rootPath = uploadPath.substring(0, uploadPath.indexOf("upload"));
			//获取文件路径  rootpath+ 年 + 月 + 日
			String fileDir_currYear_currMonth_currDay = CommMethod.getFieDir(rootPath);
			//获取文件名
			String fileName = "push_"+GetWaterId.getWaterId()+"."+file.getFileName().split("\\.")[1];
			
			String fileReallyPath = fileDir_currYear_currMonth_currDay+File.separator+fileName;
			
			File fileReally = new File(fileReallyPath);
			fis = new FileInputStream(file.getFile());
			fos = new FileOutputStream(fileReally);
			//获取文件大小
			length = fis.available();
			// 创建缓存区域
			byte[] buffer = new byte[length];
			// 将文件中的数据写入缓存数组
			fis.read(buffer);
			// 将缓存数组中的数据输出到文件
			fos.write(buffer);
			file.getFile().delete();
			
			//
			BlogContent blogContentByPK = BlogContentDao.me.geetBlogContentByPK(serialId);
			
			result = fileReallyPath.substring(fileReallyPath.indexOf("fileupload"));
			System.out.println(result);
			blogContentByPK.setReqUrl(result);
			blogContentByPK.update();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
            if (fis != null && fos != null) {
                try {
                    fis.close(); // 关闭流
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
		
		
		
		return result;
	}



	

}
